/* nav下拉跳转 */
$("#nav div ul").on('click', 'li', function () {
    localStorage.setItem('pclass', $(this).attr('pclass'));
    localStorage.setItem('pid', $(this).attr('pid'));
    location.href = './infor.html';
})

/* header 登录转换 */
let uidn = localStorage.getItem('uid');
if (uidn != null) {
    let username = localStorage.getItem('username');
    $('#login').html(username).click(function(){
        var userRes = confirm('是否要退出登录');
        if(userRes == true){
            localStorage.removeItem('uid');
            localStorage.removeItem('username');
            location.href = './res.html';
        }
    })
}else{
    $('#login').html('登录').click(function(){
        location.href = './login.html'
    })
}


$.validator.setDefaults({
    submitHandler: function () {
        let username = $('form #username').val();
        let password = $('form #password').val();
        var flag = true;
        axios.get('http://localhost:3000/users')
            .then(function (res) {
                console.log(res.data)
                res.data.forEach(v => {
                    if (v.username == username) {
                        flag = false;
                        console.log(v.username)
                    }
                });
                if (flag == true) {
                    axios.post('http://localhost:3000/users', { username, password })
                        .then(function (res) {
                            console.log(res)
                            var res = confirm('注册成功!是否要跳回首页');
                            if (res == true) {
                                location.href = '../index.html';
                            } else {
                                location.href = '../pages/res.html';
                            }
                        })
                } else {
                    alert('用户名已存在!')
                    return false;
                }
            })
    }
});
$().ready(function () {
    // 在键盘按下并释放及提交后验证提交表单
    $("#signupForm").validate({
        rules: {
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 6
            },
            confirm_password: {
                required: true,
                minlength: 6,
                equalTo: "#password"
            },
            agree: "required"
        },
        messages: {
            username: {
                required: "请输入用户名",
                minlength: "用户名必需由两个字符组成"
            },
            password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字符"
            },
            confirm_password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字符",
                equalTo: "两次密码输入不一致"
            },
            agree: "请接受我们的声明"
        }
    })
});